// pages/shipping-address/shipping-address.js
import { URL, SER_KEY } from "../../config.js"
import { POST, GET } from "../../utils/request.js"
var event = require('../../utils/event.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressList:[],
    type:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options&&options.type){
      this.setData({
        type:Number(options.type)
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAddress()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  getAddress(){
    let that = this;
    let url = URL.HTTP_BASE_INTRANET + SER_KEY.SER_KEY13;
    let data = {
      appid: getApp().globalData.appId,
      sessionId: wx.getStorageSync('session_id')
    };
    GET(url, data, (ret) => {
      if (ret.statusCode == 200) {
        wx.stopPullDownRefresh()
        // console.log(ret.data.address)
        let address = ret.data.address
        that.setData({
          addressList: address
        })
      } else {
        wx.showToast({
          title: '数据走丢啦，请稍后再试',
          icon: 'none'
        });
      }
    })
  },
  edit(e){
    console.log(e.currentTarget.dataset.item)
    let item = e.currentTarget.dataset.item
    wx.navigateTo({
      url: '../add-address/add-address?item='+JSON.stringify(item),
    })
  },
  chooseAddress(e){
    // console.log(e);
      event.emit('RefreshAddress', e.currentTarget.dataset.item);
      wx.navigateBack();
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getAddress()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})